package com.mingqijia.gassafety.authority.dal.handler;

import com.mingqijia.gassafety.authority.api.req.department.CreateDepartmentReqDTO;
import com.mingqijia.gassafety.authority.api.req.department.QueryDepartmentReqDTO;
import com.mingqijia.gassafety.authority.api.req.department.UpdateDepartmentReqDTO;
import com.mingqijia.gassafety.authority.api.resp.department.DepartmentRespDTO;
import com.mingqijia.gassafety.authority.dal.command.CreateDepartmentCommand;
import com.mingqijia.gassafety.authority.dal.command.QueryDepartmentCommand;
import com.mingqijia.gassafety.authority.dal.command.RemoveDepartmentCommand;
import com.mingqijia.gassafety.authority.dal.command.UpdateDepartmentCommand;
import com.mingqijia.gassafety.authority.dal.handler.HierarchyTranslatorImpl;
import com.mingqijia.gassafety.db.entity.auth.CompanyEntry;
import com.mingqijia.gassafety.db.entity.auth.HierarchyEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(HierarchyTranslatorImpl.class)
public interface HierarchyTranslator {

    DepartmentRespDTO toRespDTO(HierarchyEntry entry);

    DepartmentRespDTO toRespDTO(CompanyEntry entry);

    HierarchyEntry toEntry(CreateDepartmentCommand command);

    @Mapping(source = "orgId", target = "id")
    HierarchyEntry toEntry(UpdateDepartmentCommand command);

    QueryDepartmentCommand toCommand(QueryDepartmentReqDTO req);

    CreateDepartmentCommand toCommand(CreateDepartmentReqDTO req);

    @Mapping(source = "id", target = "orgId")
    UpdateDepartmentCommand toCommand(UpdateDepartmentReqDTO req);
    
    HierarchyEntry toEntry(RemoveDepartmentCommand command);

    default List<DepartmentRespDTO> toRespDTO(List<HierarchyEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }
}
